import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/main/login/register/custom_picker.dart';
import 'package:share_fit/app/controllers/main/login/register/register_logic.dart';
import 'package:share_fit/app/controllers/space/space_const.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/routes/routes.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/generated/locales.g.dart';

class RegisterInfoStepPage extends StatelessWidget {

  final logic = Get.put(RegisterLogic());
  int _selectIndex = 0;

  bool toMainPage = false;

  @override
  Widget build(BuildContext context) {
    if(Get.arguments!=null) {
      Map<String, dynamic> params = Get.arguments;
      if(params.containsKey('toMainPage')) {
        toMainPage = params['toMainPage'];
      }
    }
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        centerTitle: true,
        title: Text(LocaleKeys.register_info_setting_step.ts, style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
        leading: BackBtnIcon(onTap: () {
          if (Routes.isPageOpened(Routes.mainPage)) {
            Get.back();
          }
          else {
            Get.offAllNamed(Routes.mainPage);
          }
        }),
        actions: [
          GestureDetector(
            onTap: (){
              // Get.offAllNamed('/main');
              if(toMainPage) {
                Get.until((route) => Get.currentRoute=='/main');
              } else {
                Get.offAllNamed('/main');
              }
            },
            child: Container(
              margin: EdgeInsets.only(right: 40.w),
              child: Text(LocaleKeys.register_info_skip.ts, style: TextStyle(fontSize: 43.sp, color: Color(0xFF444444)),),
            ),)
        ],
      ),
      body: Column(
        children: [
          Container(
            margin: EdgeInsets.only(top: 100.h),
            height: 600.h,
            child: CustomPicker(
                unit: LocaleKeys.step.ts,
                itemExtent: 189.h,
                selectionOverlay: Container(color: Colors.white,),
                scrollController: FixedExtentScrollController(
                  initialItem: 0,
                ),
                onSelectedItemChanged: (value){
                  _selectIndex = value;
                },
                children: SpaceConst.targetSteps.map((e) {
                  return Center(
                    child: Text(e.toString(), style: TextStyle(fontSize: 64.sp),),
                  );
                }).toList()
            ),
          ),
          Spacer(),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Container(
                height: 136.h,
                width: 498.w,
                child: ElevatedButton(
                  onPressed: () {
                    Get.offNamed('/login/register/info/weight', arguments: {'toMainPage': toMainPage});
                  },
                  style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(ColorConst.secondaryFontColor),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)
                      ))
                  ),
                  child: Text(LocaleKeys.register_previous.ts, style: TextStyle(fontSize: 48.sp, color: Colors.white, ),),
                ),),
              Container(
                height: 136.h,
                width: 498.w,
                child: ElevatedButton(
                  onPressed: () {
                    logic.save('sport_target_step', SpaceConst.targetSteps[_selectIndex], global: true);
                    logic.save('sport_target_step_index', _selectIndex, global: true);
                    if(toMainPage) {
                      Get.until((route) => Get.currentRoute=='/main');
                    } else {
                      Get.offAllNamed('/main');
                    }
                  },
                  style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(ColorConst.activeColor),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)
                      ))
                  ),
                  child: Text(LocaleKeys.register_info_completed.ts, style: TextStyle(fontSize: 48.sp, color: Colors.white, ),),
                ),)
            ],
          ),
          SizedBox(height: 216.h,)
        ],
      ),
    );
  }
}